﻿package {
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	
	import com.mixmedia.utils.MovieClipTools;
	import com.mixmedia.view.ButtonEvt;
	import com.mixmedia.view.events.ButtonClipEvent;	

	/**
	 * @author Colin
 */
	public class ButtonClip extends MovieClip implements IButtonClip{
		public var _hitArea:SimpleButton;
		private var base:ButtonEvt;
		private var count:int;
		private var _enabled : Boolean = true;
		private var baseIsDown:Boolean = false;

        public var repeatPerFrame:int = 0;
        
		public function ButtonClip() {
		    _hitArea = MovieClipTools.findButton(this);
		
		    base = new ButtonEvt(_hitArea);
		    base.when(ButtonClipEvent.MOUSE_DOWN,this,down);
		    base.when(ButtonClipEvent.MOUSE_UP,this,up);
		    this.addEventListener(Event.ENTER_FRAME, loop);
		}
		
		public function addElement(element : IButtonElement) : void {
		        base.addElement(element);
		}
		
		public function getElements():Array{
		        return base.getElements();
		}
		
		public function select() : void {
		        base.select();
		}
		
		public function when(eventType : String, whichObject : Object, callFunction : Function) : void {
		    addEventListener(eventType, callFunction, false, 0, true);
		}
		
		public function setMouseOverDelay(num : Number) : void {
		        base.setMouseOverDelay(num);
		}
		
		public function setMouseOutDelay(num : Number) : void {
		        base.setMouseOutDelay(num);
		}
		
		public function clearMouseOver() : void {
		        base.clearMouseOver();
		}
		
		public function clearMouseOut() : void {
		        base.clearMouseOut();
		}
		
		public function setEnabled(value:Boolean):void{
		        visible = value;
		        _enabled = value;
		}
		
		public function getEnabled():Boolean{
		        return _enabled;
		}
		
		private function loop(...e):void{
			if(baseIsDown == false)return;
			if(repeatPerFrame<=0)return;
			if(count==0){
				trace('up');
		if(_enabled==true){
			count = repeatPerFrame;
			trace('down');
				}
			}
			count--;
		}
		
		private function down(...e):void{
		    baseIsDown = true;
		    count=repeatPerFrame;
		}
		
		private function up(...e):void{
		    baseIsDown = false;
		}
	}
}